package walace.stocktracer;

import java.util.ArrayList;
import java.util.List;

import android.util.Log;

class StockWidgetItem extends StockItem
{
	private static final String TAG = "StockTracerItem";
	public boolean Test()
	{
		Log.d(TAG," Test start !");
		
        String str_error_msg;

		List<StockItem> items_list = new ArrayList<StockItem>();
		
		StockWidgetItem tracer_item = new StockWidgetItem();
    	tracer_item.mAppWidgetId = 7;	        	
    	tracer_item.sym = "3041.TW";           	
    	items_list.add(tracer_item);
    	
    	tracer_item = new StockWidgetItem();
    	tracer_item.mAppWidgetId = 9;	        	
    	tracer_item.sym = "5675.TW";           	
    	items_list.add(tracer_item);
    	
    	tracer_item = new StockWidgetItem();
    	tracer_item.mAppWidgetId = 11;	        	
    	tracer_item.sym = "2498.TW";           	
    	items_list.add(tracer_item);
    	
		StockDatabase stock = new StockDatabase();
		if(!stock.UpdateItems(items_list))
		{
			str_error_msg = stock.GetErrorMessage();
			Log.e(TAG, "GetInfoItems test failed:" + str_error_msg);
			return false;
		} else {
			for(StockItem item_tmp:items_list)
				Log.d(TAG, item_tmp.sym + "," + item_tmp.name + "," + item_tmp.price + "," + item_tmp.change_percent + "," + item_tmp.change);	
			Log.d(TAG, "GetInfoItems test ok");
		}	
		
		StockItem item = FindItem(items_list,9);
		if(item ==null || !item.sym.equals("2330.TW"))
		{
			Log.e(TAG, "FindItem() test FAIL");
			return false;
		}
		
		item = RemoveItem(items_list,9);
		if(item ==null || !item.sym.equals("2330.TW"))
		{
			Log.e(TAG, "RemoveItem() test FAIL");
			return false;
		}	
		
		if(items_list.size() != 2)
		{
			Log.e(TAG, "RemoveItem() test size is wrong,");
			return false;
		}
		Log.d(TAG, "Test done !!");	
		return true;		
	}
	
	public int mAppWidgetId = -1;
	public StockWidgetItem(String sym) {
		super(sym);
		mAppWidgetId = -1;
		// TODO Auto-generated constructor stub
	}		
	public StockWidgetItem(String sym,int WidgetId) {
		super(sym);
		mAppWidgetId = WidgetId;
		// TODO Auto-generated constructor stub
	}	
	public StockWidgetItem() {
		super("");
		mAppWidgetId = -1;
		// TODO Auto-generated constructor stub
	}	
	
	public static StockItem FindItem(List<StockItem> items, int appWidgetId) {
		// TODO Auto-generated method stub
		for(StockItem item:items)
		{		
			if(((StockWidgetItem)item).mAppWidgetId == appWidgetId)
			{
				return item;
			}
		}	
		return null;
	}		
	
	public static StockItem RemoveItem(List<StockItem> items, int appWidgetId) {		
		StockItem target_item = FindItem(items,appWidgetId);
		if(target_item != null)
			items.remove(target_item);
		return target_item;
	}	
	
	public static List<StockItem> CloneItemList(List<StockItem> items) {		
		List<StockItem> ret_list = new ArrayList<StockItem>();
		
		for(StockItem item:items)
		{
			ret_list.add(item);
		}
		return ret_list;
	}	
	
	public static void updateItemsList(List<StockItem> list, List<StockItem> add_list, List<StockItem> remove_list)
	{
		Log.d(TAG, "updateItemList() start");
		
		if(!remove_list.isEmpty())
		{
			StockItem target_item;
			for(StockItem item:remove_list)
			{
				target_item = StockWidgetItem.FindItem(list,((StockWidgetItem)item).mAppWidgetId);
				if(target_item != null)
					list.remove(target_item);
			}
			remove_list.clear();			
		}
		
		if(!add_list.isEmpty())
		{			
			for(StockItem item:add_list)
			{
				list.add(item);
			}
			add_list.clear();	
		}

		Log.d(TAG, "updateItemList() end");
	}	
}
